import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  ParseIntPipe,
  Put,
  Req,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { UserRelationService } from './user-relation.service';
import {
  CreateUserRelationDto,
  UpdateUserRelationDto,
} from './dto/user-relation.dto';
import { ApiTags } from '@nestjs/swagger';

@ApiTags('小程序/关联用户')
@Controller('app/user-relation')
export class UserRelationController {
  constructor(private readonly userRelationService: UserRelationService) {}

  /**
   * 创建关联关系
   */
  @Post()
  createRelation(@Req() req: Request, @Body() data: CreateUserRelationDto) {
    if (!req.user?.userId) {
      throw new HttpException('用户未登录', HttpStatus.UNAUTHORIZED);
    }
    return this.userRelationService.createRelation(req.user?.userId, data);
  }

  /**
   * 关联关系获取
   */
  @Get()
  async getUserRelations(@Req() req: Request) {
    if (!req.user?.userId) {
      throw new HttpException('用户未登录', HttpStatus.UNAUTHORIZED);
    }
    return this.userRelationService.getUserRelations(req.user?.id);
  }

  /**
   * 关联关系删除
   */
  @Delete()
  // ParseIntPipe 用于将字符串参数转换为整数
  async deleteRelation(@Req() req: Request) {
    if (!req.user?.userId) {
      throw new HttpException('用户未登录', HttpStatus.UNAUTHORIZED);
    }
    return this.userRelationService.deleteRelation(req.user?.userId);
  }
}
